package com.cipta.lomob.fragments;

import java.util.LinkedList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.cipta.lomob.R;
import com.cipta.lomob.adapter.AdapterPopularUser;
import com.cipta.lomob.base.BaseFragment;
import com.cipta.lomob.model.Users;
import com.cipta.lomob.utils.SampleData;

public class UserFollowFragment extends BaseFragment{
	private ListView lvUserFollower, lvUserFollowing;
	private LinkedList<Users> listUser;
	private ProgressBar indicator;
	private Button btnFollowing, btnFollower;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initializeLibs();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_user_follow, container, false);
		initializeViews(view);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		initializeProcess();
		initializeActions();
	}
	
	@Override
	public void initializeViews(View view) {
		// TODO Auto-generated method stub
		super.initializeViews(view);
		setHasOptionsMenu(true);
		
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		
		lvUserFollower = (ListView)view.findViewById(R.id.lv_user_follower);
		lvUserFollowing = (ListView)view.findViewById(R.id.lv_user_following);
		indicator = (ProgressBar)view.findViewById(R.id.pb_indicator);
		btnFollower = (Button)view.findViewById(R.id.btn_user_follow_follower);
		btnFollowing = (Button)view.findViewById(R.id.btn_user_follow_following);
	}
	
	@Override
	public void initializeLibs() {
		super.initializeLibs();
		
		listUser = new LinkedList<Users>();
	}
	
	@Override
	public void initializeProcess() {
		super.initializeProcess();
		
		showFollowerList();
	}
	
	private void showFollowerList(){
		indicator.setVisibility(View.GONE);
		lvUserFollower.setVisibility(View.VISIBLE);
		lvUserFollowing.setVisibility(View.GONE);
		btnFollower.setSelected(true);
		btnFollower.setBackgroundResource(R.drawable.tab_on_category);
		btnFollowing.setBackgroundResource(R.drawable.tab_off_category);
		btnFollowing.setSelected(false);
		Users users = null;
		for (int i = 0; i < SampleData.samplePopularUser.length; i++) {
			users = new Users();
			users.setName(SampleData.samplePopularUser[i][0]);
			users.setTotalMomments(Integer.parseInt(SampleData.samplePopularUser[i][1]));
			users.setTotalFollowing(Integer.parseInt(SampleData.samplePopularUser[i][2]));
			users.setTotalFollowers(Integer.parseInt(SampleData.samplePopularUser[i][3]));
			users.setFollowing(Boolean.parseBoolean(SampleData.samplePopularUser[i][4]));
			users.setAvatar("");
			
			listUser.add(users);
		}
		
		AdapterPopularUser adapterFollower = new AdapterPopularUser(getActivity(),
				listUser);
		lvUserFollower.setAdapter(adapterFollower);
	}
	
	private void showFollowingList(){
		indicator.setVisibility(View.GONE);
		lvUserFollower.setVisibility(View.GONE);
		lvUserFollowing.setVisibility(View.VISIBLE);
		btnFollower.setSelected(false);
		btnFollowing.setSelected(true);
		btnFollower.setBackgroundResource(R.drawable.tab_off_category);
		btnFollowing.setBackgroundResource(R.drawable.tab_on_category);
		Users users = null;
		for (int i = 0; i < SampleData.samplePopularUser.length; i++) {
			users = new Users();
			users.setName(SampleData.samplePopularUser[i][0]);
			users.setTotalMomments(Integer.parseInt(SampleData.samplePopularUser[i][1]));
			users.setTotalFollowing(Integer.parseInt(SampleData.samplePopularUser[i][2]));
			users.setTotalFollowers(Integer.parseInt(SampleData.samplePopularUser[i][3]));
			users.setFollowing(Boolean.parseBoolean(SampleData.samplePopularUser[i][4]));
			users.setAvatar("");
			
			listUser.add(users);
		}
		
		AdapterPopularUser adapterFollowing = new AdapterPopularUser(getActivity(),
				listUser);
		lvUserFollowing.setAdapter(adapterFollowing);
	}
	
	@Override
	public void initializeActions() {
		super.initializeActions();
		
		btnFollower.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showFollowerList();
			}
		});
		
		btnFollowing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showFollowingList();
			}
		});
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.menu_user_follow, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId()==R.id.action_invite) {
			Toast.makeText(getActivity(), "To Invite", Toast.LENGTH_LONG).show();
		}
		
		if (item.getItemId()==android.R.id.home) {
			Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
		}
		
		return super.onOptionsItemSelected(item);
	}
}
